package com.lhl.api.autoconfigure;

import com.lhl.api.advice.ApiResponseDataAdvice;
import com.lhl.api.config.ApiFilterConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @className: ApiAutoConfiguration
 * @description: TODO 类描述
 * @author: king
 * @date: 2020-09-09 11:11
 **/
@Configuration
@ConditionalOnClass(ApiResponseDataAdvice.class)
@EnableConfigurationProperties(ApiProperties.class)
public class ApiAutoConfiguration {

    @Autowired
    private ApiProperties apiProperties;

    @Bean
    @ConditionalOnMissingBean
    public ApiFilterConfig apiConfig() {
        List<String> adviceFilterPackage =  apiProperties.getAdviceFilterClass();
        List<String> adviceFilterClass  = apiProperties.getAdviceFilterClass();
        ApiFilterConfig apiConfig = new ApiFilterConfig();
        apiConfig.setAdviceFilterClass(adviceFilterClass);
        apiConfig.setAdviceFilterPackage(adviceFilterPackage);
        return apiConfig;
    }

    @Bean
    @ConditionalOnMissingBean
    public ApiResponseDataAdvice greeter(ApiFilterConfig apiFilterConfig) {
        return new ApiResponseDataAdvice(apiFilterConfig);
    }
}